package com.vip.seata.at.business.controller;

import com.vip.seata.at.business.service.BusinessService;
import com.vip.seata.at.business.tcc.MallTccService;
import com.vip.seata.at.business.util.SleuthHelper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.index.qual.SameLen;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    BusinessService businessService;

    @Resource
    MallTccService iMallTccService;



    //使用Seata
    @PostMapping("purchase")
    public String purchase(String userId, String commodityCode, int orderCount) {
        log.info("purchase start ...");
        //businessService.purchase(userId, commodityCode, orderCount);
        businessService.purchase(userId, commodityCode, orderCount);
        log.info("purchase end ...");
        return "success";
    }

    @GetMapping("/tcc")
    @ResponseBody
    public String tcc(String orderId,String userId) throws Exception {
        log.info("Sleuth traceId:{}", SleuthHelper.getSleuthTraceId());
        iMallTccService.doBusiness(orderId,userId);
        return "success";
    }

}
